package com.company.struct;

import com.company.TreeNode;

import java.util.Stack;

/**
 * @author jrue
 * @description
 * @date 2019/11/14 16:30
 */
public class CountNodes {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        Stack<TreeNode> treeNodes = new Stack<>();
        treeNodes.push(root);
        int i = 0;
        while (!treeNodes.isEmpty()) {
            TreeNode pop = treeNodes.pop();
            i++;
            if (pop.left != null) treeNodes.push(pop.left);
            if (pop.right != null) treeNodes.push(pop.right);
        }
        return i;
    }

}
